package com.sike.controller;

import com.sike.entity.Drug;
import com.sike.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/drugs")
public class DrugController {
    private final DrugService drugService;

    @Autowired
    public DrugController(DrugService drugService) {
        this.drugService = drugService;
    }

    // 查询所有药品
    @GetMapping
    public ResponseEntity<List<Drug>> findAllDrugs() {
        List<Drug> drugs = drugService.findAllDrugs();
        return ResponseEntity.ok(drugs);
    }


    // 根据id查询药品
    @GetMapping("/{id}")
    public ResponseEntity<Drug> findDrugById(@PathVariable Integer id) {
        Drug drug = drugService.findDrugById(id);
        return ResponseEntity.ok(drug);
    }

    // 搜索药品
    @GetMapping("/search")
    public ResponseEntity<List<Drug>> searchDrugs(@RequestParam String name) {
        List<Drug> drugs = drugService.searchDrugsByName(name);
        return ResponseEntity.ok(drugs);
    }

    // 添加新药品，前端暂定
    @PostMapping("/add")
    public ResponseEntity<?> addDrug(@RequestBody Drug drug) {
        drugService.addDrug(drug);
        return ResponseEntity.ok().build();
    }

    // 更新药品信息
    @PutMapping("/update")
    public ResponseEntity<?> updateDrug(@RequestBody Drug drug) {
        drugService.updateDrug(drug);
        return ResponseEntity.ok().build();
    }

    // 删除药品，前端暂定
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<?> deleteDrug(@PathVariable Integer id) {
        drugService.deleteDrug(id);
        return ResponseEntity.ok().build();
    }
}


